using System;
using ApiText.Api.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ApiText.Api.Db;



namespace ApiText.Api.Repository
{
    public class EfRepository<T> : IRepository<T> where T : BaseModel
    {
        private ApiTextDb _db;

        //构造函数，初始化中间件
        public EfRepository(ApiTextDb db)
        {
            _db = db;
        }

        //私有的 
        private DbSet<T> _entity;
        protected DbSet<T> Entity
        {
            get
            {
                if (_entity == null)
                {
                    _entity = _db.Set<T>();
                }
                return _entity;
            }
        }


        //集成查询返回一个表数据
        public IQueryable<T> Table
        {
            get
            {
                //AsQueryable，将一个IEnumerable转化为IQueryable（对应的实现类型为EnumerableQuery）。
                return Entity.AsQueryable<T>();
            }
        }

        //删除
        public void Delete(object Id)
        {

            var t = Entity.Find(Id);

            if (t == null)
            {
                //抛出空值的提示
                // throw new System.ArgumentNullException(nameof(t));

            }

            //删除通过id查找的的记录
            _db.Remove(t);

            //把数据保存到数据库
            _db.SaveChanges();
        }

        //批量删除
        public void DeleteBulk(IEnumerable<object> ids)
        {

            //定以一个删除的集合
            var dList = new List<int>();

            //把获取到的集合中的每一个元素都遍历出来，并添加到集合中
            foreach (var item in ids)
            {
                var temp = (int)item;
                dList.Add(temp);
            }

            var ts = Entity.Where(x => dList.Contains(x.Id)).ToList();

            if (ts == null)
            {
                //抛出空值的提示
                throw new System.ArgumentNullException(nameof(ts));
            }

            //批量删除通过查找到的记录
            _db.RemoveRange(ts);

            //把数据保存到数据库
            _db.SaveChanges();
        }

        public T GetById(object Id)
        {

            //通过查找id获取到对应的实体对象
            return Entity.Find(Id);
        }

        //添加
        public void Insert(T entity)
        {
            if (entity == null)
            {
                throw new System.ArgumentNullException();
            }

            //设置创建初始值
            entity.IsActived = true;
            entity.IsDeleted = false;
            entity.CreatedTime = DateTime.Now;
            entity.UpdatedTime = DateTime.Now;
            entity.DisplayOrder = 0;

            // 插入数据
            Entity.Add(entity);

            _db.SaveChanges();

        }

        //异步添加
        public async Task InsertAsync(T entity)
        {

            if (entity == null)
            {
                throw new System.ArgumentNullException();
            }

            //设置创建初始值
            entity.IsActived = true;
            entity.IsDeleted = false;
            entity.CreatedTime = DateTime.Now;
            entity.UpdatedTime = DateTime.Now;
            entity.DisplayOrder = 0;


            await Entity.AddAsync(entity);
            await _db.SaveChangesAsync();
        }

        //批量添加
        public void InsertBulk(IEnumerable<T> entities)
        {

            if (entities == null)
            {
                throw new System.ArgumentNullException();
            }


            foreach (var entity in entities)
            {
                entity.IsActived = true;
                entity.IsDeleted = false;
                entity.CreatedTime = DateTime.Now;
                entity.UpdatedTime = DateTime.Now;
                entity.DisplayOrder = 0;
            }
            Entity.AddRange(entities);
            _db.SaveChanges();

        }

        //异步批量添加
        public async Task InsertBulkAsync(IEnumerable<T> entities)
        {
            if (entities == null)
            {
                throw new System.ArgumentNullException();
            }


            foreach (var entity in entities)
            {
                entity.IsActived = true;
                entity.IsDeleted = false;
                entity.CreatedTime = DateTime.Now;
                entity.UpdatedTime = DateTime.Now;
                entity.DisplayOrder = 0;
            }

            await Entity.AddRangeAsync(entities);
            await _db.SaveChangesAsync();


        }


        //修改数据
        public void Update(T entity)

        {

            if (entity == null)
            {
                throw new System.ArgumentNullException();
            }

            entity.UpdatedTime = DateTime.Now;

            //将修改实体对象entity 更新到数据库
            Entity.Update(entity);

            _db.SaveChanges();
        }

        public void UpdateBulk(IEnumerable<T> entity)
        {
            throw new NotImplementedException();
        }


    }
}